package com.tienphong.clipxonxao;

import org.json.JSONObject;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.parse.ParseBroadcastReceiver;

public class MyReceiver extends ParseBroadcastReceiver {
	NotificationCompat.Builder mBuilder;
	Intent resultIntent;
	int mNotificationId = 001;
	Uri notifySound;

	String alert; // This is the m

	@Override
	public void onReceive(Context context, Intent intent) {

		Log.d("onReceive", "onReceive");
		// Get JSON data and put them into variables
		if (intent == null)
			return;

		Bundle extras = intent.getExtras();

		if (extras == null)
			return;
		// Log.d("extrasData", intent.getStringExtra("com.parse.Data"));

		try {
			// JSONObject jsonObject = new JSONObject();
			// jsonObject.put("title",
			// "Trai Tây hát ‘Nắng Ấm Xa Dần’ nghe nổi da gà");
			// jsonObject.put("content", "Click là thích");
			// jsonObject
			// .put("url",
			// "http://clipxonxao.vn/video/trai-tay-hat-nang-am-xa-dan-nghe-noi-da-ga/");
			// Log.d("jsonObject", jsonObject.toString());
			//

			JSONObject json = new JSONObject(intent.getStringExtra("data"));
			Log.d("json",json.toString());
			
			notifySound = RingtoneManager
					.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

			mBuilder = new NotificationCompat.Builder(context);
			mBuilder.setSmallIcon(R.drawable.ic_launcher); // You can change
															// your
															// icon
			mBuilder.setContentText(json.getString("content"));
			mBuilder.setContentTitle(json.getString("alert"));
			mBuilder.setSound(notifySound);
			mBuilder.setAutoCancel(true);

			// this is the activity that we will send the user, change this to
			// anything you want
			resultIntent = new Intent(context, MainActivity.class);
			resultIntent.putExtra("url", json.getString("url"));
			PendingIntent resultPendingIntent = PendingIntent
					.getActivity(context, 0, resultIntent,
							PendingIntent.FLAG_UPDATE_CURRENT);

			mBuilder.setContentIntent(resultPendingIntent);

			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(context.NOTIFICATION_SERVICE);

			notificationManager.notify(mNotificationId, mBuilder.build());

		} catch (Exception e) {
			e.printStackTrace();

		}

		// You can specify sound

		super.onReceive(context, intent);
	}

}
